/***************************************************************************//**
 * @file MSc_Integrator.h
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2013 Marek M. Cel
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/
#ifndef MSC_INTEGRATOR_H
#define MSC_INTEGRATOR_H

////////////////////////////////////////////////////////////////////////////////

#include "MSc_Exception.h"

////////////////////////////////////////////////////////////////////////////////

/** Abstract numerical integration template class. */
template <class TYPE>
class MSc_Integrator
{
public:

    /**
     * Constructor.
     * @param size of vector
     * @param object pointer
     * @param pointer to function which calculates vector derivative and takes current vector as first argument and resulting vector derivative as second
     */
    MSc_Integrator( int size = 0, TYPE *obj = 0, void (TYPE::*fun)(const double *, double *) = 0 ) :
        m_size ( size ),

        m_obj ( obj ),
        m_fun ( fun )
    {
        if ( m_size <= 0 )
        {
            MSc_Exception e;

            e.setType( MSc_Exception::UnknownException );
            e.setFile( __FILE__ );
            e.setLine( __LINE__ );
            e.setInfo( "m_vectSize <= 0" );

            throw e;
        }

        if ( m_obj == NULL )
        {
            MSc_Exception e;

            e.setType( MSc_Exception::NullPointer );
            e.setFile( __FILE__ );
            e.setLine( __LINE__ );
            e.setInfo( "m_obj == NULL" );

            throw e;
        }

        if ( m_fun == NULL )
        {
            MSc_Exception e;

            e.setType( MSc_Exception::NullPointer );
            e.setFile( __FILE__ );
            e.setLine( __LINE__ );
            e.setInfo( "m_fun == NULL" );

            throw e;
        }
    }

    /**
     * Integrates given vector.
     * This is interface abstract method.
     * @param step integration time step [s]
     * @param vect integrating array
     */
    virtual void integrate( double step, double *vect ) = 0;

protected:

    const int m_size;   ///< size of vector

    /** Calls function calculating derivative of the given vector. */
    inline void fun( const double *x_0, double *x_dot )
    {
        (m_obj->*m_fun)( x_0, x_dot );
    }

private:

    TYPE *const m_obj;                              ///< object pointer
    void (TYPE::*m_fun)(const double *, double *);  ///< function pointer

    /** Using this constructor is forbidden. */
    MSc_Integrator( const MSc_Integrator & ) {}
};

////////////////////////////////////////////////////////////////////////////////

#endif // MSC_INTEGRATOR_H
